package activity.personal;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import activity.personal.qianbao.ChongZhiActivity;
import activity.personal.qianbao.JiaoYiJiLuActivity;
import activity.personal.qianbao.TiXianActivity;
import activity.personal.qianbao.TiXianToZhiFuBaoActivity;
import activity.personal.qianbao.ZhiFuPassActivity;
import activity.personal.qianbao.ZhiFuPassSheZhiActivity;
import base.BaseActivity;
import base.MyApplcation;
import bean.personal_bean.AccountBean;
import bean.personal_bean.ZhiFuBaoBandingTypebean;
import presenter.SysTemPresenter;
import util.utls.ByAlert;
import util.utls.Const;
import util.utls.IsUtilUid;
import util.utls.SPUtils;
import util.volley.ResultEntity;

/**
 * 钱包
 */
public class ActivityQianBao extends BaseActivity implements Observer {

    private TextView tvTitle, tvTest;
    private ImageView back;
    private RelativeLayout qianbao_yinhangka, qianbao_chongzhi, relayouttitle, qianbao_tixian, qianbao_zhangdan;
    private TextView qianbao_money;
    SysTemPresenter presenter;
    List<ZhiFuBaoBandingTypebean> typebean;
    AccountBean bean;
    RelativeLayout layout;
    TextView qianbao_line;
    PopupWindow mPopWin;

    private String typeId;//账号绑定ID
    private String AcctiId;//账号绑定ID

    EditText dialog_et;
    private Dialog dialog;// 自定义Dialog

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ny_qian_bao_activity);
        tvTitle.setText("我的钱包");
        tvTest.setText("设置密码");
        tvTitle.setTextColor(getResources().getColor(R.color.white));
        setStatusBarColor(R.color.main_color);
        bean = new AccountBean();
        typebean = new ArrayList<ZhiFuBaoBandingTypebean>();
        presenter = new SysTemPresenter(this);

        showExtraDialog();

        presenter.onBangDinType();
        if (IsUtilUid.isPay() == null || IsUtilUid.isPay() == "") {

            Intent intent = new Intent(MyApplcation.ctx, ZhiFuPassSheZhiActivity.class);
            startActivity(intent);
        }

        relayouttitle.setBackgroundColor(getResources().getColor(R.color.main_color));
        //昵称更改的广播
        IntentFilter heada = new IntentFilter("uiAlertView");
        registerReceiver(AddreHead, heada);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(AddreHead);
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.onAccount(IsUtilUid.isUid());
    }

    BroadcastReceiver AddreHead = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            presenter.onAccount(IsUtilUid.isUid());

        }

        ;
    };

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.qianbao_tixian:
//                tixianPop(v, typeId);
//                showExtraDialog();
                dialog.show();
                break;
            case R.id.back:
                finish();
                break;
            case R.id.qianbao_zhangdan:
                intent = new Intent(this, JiaoYiJiLuActivity.class);
                startActivity(intent);
                break;
            case R.id.qianbao_yinhangka:
                //绑定银行卡
                intent = new Intent(MyApplcation.ctx, ActivityZhuangDan.class);
                startActivity(intent);
                break;
            case R.id.qianbao_chongzhi:
                //充值
              //  onChongZhi(v, AcctiId);
                intent = new Intent(this, ChongZhiActivity.class);
                startActivity(intent);
                break;
            case R.id.tvTest:
                //密码
                intent = new Intent(this, ZhiFuPassActivity.class);
                startActivity(intent);

                break;
        }
    }
    /**
     * 自定义Dialog
     */
    /**
     * 自定义Dialog
     */
    private void showExtraDialog() {
        dialog = new Dialog(ActivityQianBao.this, R.style.rel_dialog);
        View view = LayoutInflater.from(ActivityQianBao.this).inflate(
                R.layout.dialog_normal_layout, null);
        dialog.setContentView(view);
        dialog_et = (EditText) view.findViewById(R.id.dialog_et);

        TextView user_title = (TextView) view.findViewById(R.id.user_title);
        user_title.setText("输入您的6位支付密码");
        dialog_et.setText("");

        TextView dialog_ok = (TextView) view.findViewById(R.id.dialog_ok);
        TextView dialog_quxiao = (TextView) view.findViewById(R.id.dialog_quxiao);
        final String rele = (String) SPUtils.get(MyApplcation.ctx, null, Const.ZHIFUMIMA, "");
        dialog.setCanceledOnTouchOutside(false);
        dialog_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.dismiss();

            }
        });
        dialog_quxiao.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {


                if (dialog_et.getText().toString().equals("")) {
                    ByAlert.alert("请填写支付密码");
                } else {
                    showProgressDialog("加载中...", true);
                    presenter.oncheckSecret(IsUtilUid.isUid(), dialog_et.getText().toString());
                }


            }
        });
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        // 屏幕宽度
        int screenWidth = display.getWidth();
        // 屏幕高度
        int screenHeight = display.getHeight();
        Window window = dialog.getWindow();

        WindowManager.LayoutParams lp = window.getAttributes();
        window.setGravity(Gravity.CENTER);
        lp.width = screenWidth * 3 / 4;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

    }


    /**
     * 提现选择
     */
    private void tixianPop(View v, final String typeId) {
        layout = (RelativeLayout) LayoutInflater.from(this).inflate(
                R.layout.item_tixian_xuanze, null);
        TextView tixian_zhifubao = (TextView) layout.findViewById(R.id.tixian_zhifubao);
        TextView tixian_yinhangka = (TextView) layout.findViewById(R.id.tixian_yinhangka);

        mPopWin = new PopupWindow(layout,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mPopWin.setBackgroundDrawable(new BitmapDrawable());
        mPopWin.showAsDropDown(relayouttitle, 5, 1);
        mPopWin.update();

        tixian_zhifubao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityQianBao.this, TiXianToZhiFuBaoActivity.class);
                intent.putExtra("typeId", typeId);
                startActivity(intent);
                mPopWin.dismiss();
            }
        });
        tixian_yinhangka.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityQianBao.this, TiXianActivity.class);
                startActivity(intent);
                mPopWin.dismiss();
            }
        });
    }

    /**
     * 充值
     */
    private void onChongZhi(View v, final String AcctiId) {
        layout = (RelativeLayout) LayoutInflater.from(this).inflate(
                R.layout.item_tixian_chongzhi, null);
        TextView tixian_zhifubao = (TextView) layout.findViewById(R.id.tixian_zhifubao);
        TextView tixian_yinhangka = (TextView) layout.findViewById(R.id.tixian_yinhangka);

        mPopWin = new PopupWindow(layout,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mPopWin.setBackgroundDrawable(new BitmapDrawable());
        mPopWin.showAsDropDown(qianbao_line, 5, 1);
        mPopWin.update();

        tixian_zhifubao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityQianBao.this, ActivityChongZhi.class);
                startActivity(intent);
                mPopWin.dismiss();
            }
        });
        tixian_yinhangka.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityQianBao.this, TiXianActivity.class);
                startActivity(intent);
                mPopWin.dismiss();
            }
        });
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == presenter.Account_success) {
                bean = (AccountBean) entity.getData();
                double in = Double.parseDouble(bean.getTotal());
                qianbao_money.setText(in / 100 + "元");
                SPUtils.put(ActivityQianBao.this, null,
                        Const.QIAN, bean.getTotal());
            }
            if (entity.getEventType() == presenter.Secret_success) {
//                ByAlert.alert("" + entity.getData());
                Intent intent = new Intent(ActivityQianBao.this, TiXianToZhiFuBaoActivity.class);
                intent.putExtra("typeId", typeId);
                startActivity(intent);
                dialog_et.setText("");
                dialog.dismiss();
            }
            if (entity.getEventType() == presenter.BangdingType_success) {
                typebean = (List<ZhiFuBaoBandingTypebean>) entity.getData();
                typeId = typebean.get(0).getId();

                SPUtils.put(MyApplcation.ctx, null,
                        Const.ISZID, typebean.get(0).getId());
                Log.d("tytpID", typeId);
            }
        }
        try {
            closeProgressDialog();

        } catch (Exception e) {
        }
    }
}
